package cn.li.security.json.handle;

import cn.li.security.json.bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthenticationFailJSONHandler extends BaseJSONHandler implements AuthenticationFailureHandler {

    public static final String LOGIN_FAIL_MSG = "LOGIN_FAIL";

    public AuthenticationFailJSONHandler(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Result r = new Result(10001, LOGIN_FAIL_MSG,e.getLocalizedMessage());
        this.writeResult(response,r);
    }
}